package com.announcement.util;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.junit.Test;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * html邮件
 * 
 * @author chen
 * 
 */
public class JavaMail {
	/**
	 * 
	 * @param emailTitle
	 *            标题
	 * @param emailContent
	 *            内容
	 * @param email
	 *            email地址
	 * @throws MessagingException
	 */
	public static void sendMail(String emailTitle, String emailContent,
			String email) throws MessagingException {

		System.out.println(emailContent);
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

		// 设定mail server
		senderImpl.setHost("smtp.163.com");
		senderImpl.setPort(25);
		// 建立邮件消息,发送简单邮件和html邮件的区别
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
				true, "UTF-8");

		// 设置收件人，寄件人
		messageHelper.setTo(email);
		messageHelper.setFrom("18826137219@163.com");
		messageHelper.setSubject(emailTitle);
		// true 表示启动HTML格式的邮件
		messageHelper.setText(emailContent, true);
		senderImpl.setUsername("18826137219"); // 根据自己的情况,设置username
		senderImpl.setPassword("0312ssyy"); // 根据自己的情况, 设置password
		Properties properties = new Properties();
		properties.put("mail.smtp.auth", "true"); // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
		properties.put("mail.smtp.timeout", "25000");
		properties.setProperty("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		senderImpl.setJavaMailProperties(properties);
		// 发送邮件
		senderImpl.send(mailMessage);

		System.out.println("邮件发送成功..");
		/*
		 * 您的登陆信息： Email：2463610028@qq.com 重设密码链接：
		 * http://www.thinksaas.cn/index.
		 * php?app=user&ac=resetpwd&mail=2463610028
		 * 
		 * @qq.com&set=lqlh8x82vdw4cgs8ck0440kswso8cw0
		 */
	}

	@Test
	public void tests() throws MessagingException {

		sendMail("dd", "dd", "1217873870@qq.com");
	}
}